package CommodityController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/CommodityService"
	"QingDai-Gin/service/OrderService"
	"QingDai-Gin/service/ShopService"
	"QingDai-Gin/utils/JwtUtil"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

// GetAllCommodities
// @Tags 商品
// @Summary 获取所有商品
// @Router /address/all [get]
func GetAllCommodities(context *gin.Context) {
	commodity, _ := CommodityService.GetAllCommodity()
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取所有商品成功", Data: commodity})
}

// GetCommoditiesByShopId
// @Tags 商品
// @Summary 根据shopId获取商品
// @Router /commodity/shopId [get]
func GetCommoditiesByShopId(context *gin.Context) {
	shopId, _ := context.Get("shopId")
	shopId, _ = CommodityService.GetCommoditiesByP(map[string]any{"shop_id": shopId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据shopId获取商品成功", Data: shopId})
}

// CreateCommoditiesByShopIdAndJwt
// @Tags 商品
// @Summary 根据Jwt创建商品
// @Router /commodity/jwt/shopId [post]
func CreateCommoditiesByShopIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	title, _ := context.Get("title")
	price, _ := context.Get("price")
	shippingAddressId, _ := context.Get("shippingAddressId")
	wDescribe, _ := context.Get("wDescribe")
	shopId, _ := context.Get("shopId")
	number, _ := context.Get("number")
	enable, _ := context.Get("enable")

	shop, _ := ShopService.GetShopById(shopId.(int))
	//未查到
	if shop.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "shopId非法", Data: nil})
	}
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备创建给改商店创建商品的权利", Data: nil})
	}
	commodity := models.Commodity{
		//ID:                0,
		Title:             title.(string),
		Price:             price.(float64),
		ShippingAddressId: shippingAddressId.(int),
		WDescribe:         wDescribe.(string),
		ShopId:            shopId.(int),
		Number:            number.(int),
		Enable:            enable.(int),
	}
	CommodityService.CreateCommodity(commodity)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt创建商品成功", Data: commodity})
}

// UpdateCommoditiesByIdAndJwt
// @Tags 商品
// @Summary 根据id及jwt更改商品
// @Router /commodity/jwt/id [put]
func UpdateCommoditiesByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	title, _ := context.Get("title")
	price, _ := context.Get("price")
	shippingAddressId, _ := context.Get("shippingAddressId")
	wDescribe, _ := context.Get("wDescribe")
	shopId, _ := context.Get("shopId")
	number, _ := context.Get("number")
	enable, _ := context.Get("enable")
	commodityGet, _ := CommodityService.GetCommodityById(id.(int))
	if commodityGet.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "商品id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(shopId.(int))
	//未查到
	if shop.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "shopId非法", Data: nil})
	}
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备编辑该商店商品的权利", Data: nil})
	}

	commodity := models.Commodity{
		//ID:                0,
		Title:             title.(string),
		Price:             price.(float64),
		ShippingAddressId: shippingAddressId.(int),
		WDescribe:         wDescribe.(string),
		ShopId:            shopId.(int),
		Number:            number.(int),
		Enable:            enable.(int),
	}
	CommodityService.UpdateCommodity(commodity)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt更改商品成功", Data: nil})
}

// DeleteCommoditiesByIdAndJwt
// @Tags 商品
// @Summary 根据id及jwt删除商品
// @Router /commodity/jwt/id [delete]
func DeleteCommoditiesByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	commodityGet, _ := CommodityService.GetCommodityById(id.(int))
	if commodityGet.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "商品id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(commodityGet.ShopId)
	//未查到
	if shop.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "shopId非法", Data: nil})
	}
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备删除该商店商品的权利", Data: nil})
	}
	CommodityService.DeleteCommodity(models.Commodity{ID: id.(int)})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt和id删除商品成功", Data: nil})
}

// GetCommoditiesById
// @Tags 商品
// @Summary 根据id获取商品
// @Router /commodity/id [get]
func GetCommoditiesById(context *gin.Context) {
	id, _ := context.Get("id")
	commodity, _ := CommodityService.GetCommodityById(id.(int))
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id商品获取商品成功", Data: commodity})
}

// ChangeCommodityEnableByIdAndJwt
// @Tags 商品
// @Summary 根据id及jwt上下架商品
// @Router /commodity/jwt/id/enable [get]
func ChangeCommodityEnableByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	commodityGet, _ := CommodityService.GetCommodityById(id.(int))
	if commodityGet.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "商品id非法", Data: nil})
	}
	shop, _ := ShopService.GetShopById(commodityGet.ShopId)
	//未查到
	if shop.ID == 0 {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "shopId非法", Data: nil})
	}
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备上下架该商店商品的权利", Data: nil})
	}
	if commodityGet.Enable == 1 {
		CommodityService.UpdateCommodity(models.Commodity{ID: id.(int), Enable: 0})
	} else if commodityGet.Enable == 0 {
		CommodityService.UpdateCommodity(models.Commodity{ID: id.(int), Enable: 1})
	}
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据jwt和id上下架商品成功", Data: nil})
}

// GetCommoditiesByOrderIdAndJwt
// @Tags 商品
// @Summary 根据orderId及jwt获取商品
// @Router /commodity/jwt/orderId [get]
func GetCommoditiesByOrderIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	orderId, _ := context.Get("orderId")
	order, _ := OrderService.GetOrderById(orderId.(int))
	if order.BuyerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备获取该订单商品的权利", Data: nil})
	}
	commodityIdListStr := order.CommodityIdList
	commodityIdList := strings.Split(commodityIdListStr, ",")
	var commodtities []models.Commodity
	for _, commodityIdStr := range commodityIdList {
		commodityId, _ := strconv.Atoi(commodityIdStr)
		commodity, _ := CommodityService.GetCommodityById(commodityId)
		commodtities = append(commodtities, commodity)
		fmt.Println(commodity)
	}
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据商品id获取商品成功", Data: commodtities})
}
